package render

import (
	"MetroBuilderGameJam/gameEngine/drawer"
	"MetroBuilderGameJam/gameEngine/geom"
	"MetroBuilderGameJam/gameEngine/utils"
	"MetroBuilderGameJam/world"
	"fmt"
	"github.com/hajimehoshi/ebiten/v2"
	"image/color"
)

type Station struct {
	Size      float64
	LineWidth float64
	Color     color.Color
	drawer    *drawer.Drawer
	limit     int
}

func NewStationRenderer(d *drawer.Drawer, size float64, lineWidth float64, color color.Color, passengersLimit int) *Station {
	return &Station{
		Size:      size,
		LineWidth: lineWidth,
		Color:     color,
		drawer:    d,
		limit:     passengersLimit,
	}
}

func (s *Station) Draw(screen *ebiten.Image, item *world.Station) *utils.Figure {
	if item.Position() == nil {
		return utils.NoFigure()
	}
	c := uint8(min(255, float64(s.limit-len(item.Passengers()))/float64(s.limit)*1.5*255))
	bgClr := color.RGBA{R: c, G: c, B: c, A: 255}
	pos := *item.Position()
	switch item.Shape() {
	case world.StationTypeCircle:
		s.drawer.BorderedCircle(screen, pos, s.Size, s.LineWidth, s.Color, bgClr)
	case world.StationTypeSquare:
		s.drawer.BorderedSquare(screen, pos, s.Size, s.LineWidth, s.Color, bgClr)
	case world.StationTypeTriangle:
		s.drawer.BorderedSimpleTriangle(screen, pos, s.Size, s.LineWidth, s.Color, bgClr)
	case world.StationTypePentagon:
		s.drawer.BorderedPentagon(screen, pos, s.Size, s.LineWidth, s.Color, bgClr)
	case world.StationTypeStar:
		s.drawer.BorderedStar(screen, pos, s.Size, s.LineWidth, s.Color, bgClr)
	case world.StationTypeCross:
		s.drawer.BorderedCross(screen, pos, s.Size, s.LineWidth, s.Color, bgClr)
	default:
		fmt.Println(fmt.Errorf("unknown shape %d", item.Shape()))
	}

	return utils.NewFigure(item, geom.NewColliderCircle(pos, s.Size))
}
